<?php
/**
 * TomatoCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: DashboardController.php 4561 2010-08-12 11:49:30Z huuphuoc $
 * @since		2.0.0
 */

class Core_DashboardController extends Zend_Controller_Action 
{
	/* ========== Backend actions =========================================== */
	
	/**
	 * Show dashboard of administration
	 * 
	 * @return void
	 */
	public function indexAction() 
	{
		$request = $this->getRequest();
		$act  = $request->getParam('act', '');
		$conn = Tomato_Db_Connection::factory()->getMasterConnection();
		$user = Zend_Auth::getInstance()->getIdentity();
		
		/**
		 * Allow user to personalize the dashboard
		 * @since 2.0.7
		 */
		switch ($act) {
			/**
			 * Load the layout editor
			 */
			case 'edit':
				$this->_helper->getHelper('viewRenderer')->setNoRender();
				$this->_helper->getHelper('layout')->disableLayout();
				
				$moduleDirs = Tomato_Utility_File::getSubDir(TOMATO_APP_DIR . DS . 'modules');
		
				/**
				 * Get modules
				 */
				$moduleDao = Tomato_Model_Dao_Factory::getInstance()->setModule('core')->getModuleDao();
				$moduleDao->setDbConnection($conn);
				$modules = $moduleDao->getModules();
				$this->view->assign('widgetModules', $modules);
				
				$response = $this->view->render('dashboard/_editor.phtml');
				$this->getResponse()->setBody($response);
				break;
				
			/**
			 * Save the dashboard layout
			 */
			case 'save':
				$this->_helper->getHelper('viewRenderer')->setNoRender();
				$this->_helper->getHelper('layout')->disableLayout();

				$dashboradDao = Tomato_Model_Dao_Factory::getInstance()->setModule('core')->getDashboardDao();
				$dashboradDao->setDbConnection($conn);
				$dashboard = $dashboradDao->getByUser($user->user_id);
				if (null == $dashboard) {
					$dashboradDao->create(new Core_Models_Dashboard(array(
						'user_id' 	 => $user->user_id,
						'user_name'  => $user->user_name,
						'layout' 	 => $request->getParam('layout'),
						'is_default' => 0,
					)));
				} else {
					$dashboard->layout = $request->getParam('layout');
					$dashboradDao->update($dashboard);
				}
				
				$this->getResponse()->setBody('RESULT_OK');
				break;
				
			/**
			 * Load the dashboard layout
			 */
			default:
				$dashboradDao = Tomato_Model_Dao_Factory::getInstance()->setModule('core')->getDashboardDao();
				$dashboradDao->setDbConnection($conn);
				$dashboard = $dashboradDao->getByUser($user->user_id);
				if (null == $dashboard) {
					/**
					 * Try to load the default dashboard
					 */
					$dashboard = $dashboradDao->getDefault();
				}
				if ($dashboard != null) {
					$this->view->assign('dashboardLayout', $dashboard->layout);
				}
				
				/** 
				 * START: 
				 * 1. Thong tin Doanh nghiep
				 * 2. SP moi nhat 
				 * 3. Danh sach don dat hang 
				 * 4. Danh sach cong viec
				 **/
				
				/**********************************
				*	1. Thong tin Doanh Nghiep 
				**********************************/
				
				$lang = $request ->getParam('lang');
		        $this->view->assign('lang', $lang);
		
		        $conn = Tomato_Db_Connection::factory()->getMasterConnection();
		
		        /** Enterprise basic model access to db */
		        $enterpriseDao = Tomato_Model_Dao_Factory::getInstance()->setModule('enterprise')->getEnterpriseDao();
		        $enterpriseDao->setDbConnection($conn);
		
		        /**  Enterprise accont payment model access to db */
		        $paymentDao = Tomato_Model_Dao_Factory::getInstance()->setModule('enterprise')->getPaymentDao();
		        $paymentDao->setDbConnection($conn);
		
		        /** Enterprise User model access to db */
		        $userAccountDao = Tomato_Model_Dao_Factory::getInstance()->setModule('enterprise')->getUserDao();
		        $userAccountDao->setDbConnection($conn);
		
		        $pageIndex = $request->getParam('pageIndex', 1);
		        $perPage   = 20;
		        $offset    = ($pageIndex - 1) * $perPage;
		        $purifier     = new HTMLPurifier();
		        $exp = array(
		            'deleted' => 0
		        );
		        /** Get current user */
		        $user = Zend_Auth::getInstance()->getIdentity();
		        $userAccount = $userAccountDao->getByUser($user->user_id);
		        $enterprise_user_accounts = 0;
		        if(isset($userAccount->user_id))
		        	if($user->user_id == $userAccount->user_id)
		        	{
				        $enterprise_user_accounts = 1;
				        $this->view->assign("enterprise_user_accounts",$enterprise_user_accounts);
		        	}
		
		        /** Check permission all list enterprise */
		        if(!Core_Services_RuleChecker::isAllowed('all-list')){
		            $exp['id'] = $purifier->purify(@$userAccount->enterprise_id);
		        }
				
		        $sltProfession = null;
		        $sltApplication = null;
		        if ($request->isPost()) {
		            $id           = $request->getPost('id');
					$company_name = $request->getPost('company_name',null);
					$registration_number 	= $request->getPost('registration_number',null);
					$registration_date_from = $request->getPost('registration_date_from',null);
					$registration_date_to 	= $request->getPost('registration_date_to',null);
					$sltProfession			= $request->getPost('sltProfession',null);
					$sltApplication			= $request->getPost('sltApplication',null);
					if($sltApplication=="") $sltApplication = null;
		            if(Core_Services_RuleChecker::isAllowed('all-list')){
						
		                if ($id) {
		                    $exp['id'] = $purifier->purify($id);
		                }
						if($company_name){
							$exp["company_name"] = $company_name;
						}
						if($registration_number){
							$exp["registration_number"] = $registration_number;
						}
						if($registration_date_from){
							$exp["registration_date_from"] = $registration_date_from;
						}
						if($registration_date_to){
							$exp["registration_date_to"] = $registration_date_to;
						}
						if($sltProfession){
							$exp["profession"] = $sltProfession;
						}	
						if($sltApplication){
							$exp["application"] = $sltApplication;
						}
		            }
		            $params = rawurlencode(base64_encode(Zend_Json::encode($exp)));
		        } else {
					$company_name           = "";
					$registration_number 	= "";
					$registration_date_from = "";
					$registration_date_to 	= "";	
					$this->sltProfession   = null;	
		            $params = $request->getParam('q');
		            if ($params != null) {
		                $exp = rawurldecode(base64_decode($params));
		                $exp = Zend_Json::decode($exp);
		            } else {
		                $params = rawurlencode(base64_encode(Zend_Json::encode($exp)));
		            }
		        }
		
		        $enterprise = $enterpriseDao->find($offset, $perPage, $exp);
		        $total       = $enterpriseDao->count($exp);
				
				$professions = $enterpriseDao->getProfessions();
				$this->view->assign("professions",$professions);
				
		        /** Get account_valid_until_date by enterprise id */
		         $arValidDate = array();
		         foreach($enterprise as $row){
		            $payment = $paymentDao->getById($row->id);
		            if($payment){
		                $arValidDate[$row->id] = $payment;
		            }
		         }
		         $this->view->assign('arValidDate', $arValidDate);
		
		        /** Paginator */
		        $paginator = new Zend_Paginator(new Tomato_Utility_PaginatorAdapter($enterprise, $total));
		        $paginator->setCurrentPageNumber($pageIndex);
		        $paginator->setItemCountPerPage($perPage);
		
				$this->view->assign("company_name",$company_name);
				$this->view->assign("registration_number",$registration_number);
				$this->view->assign("registration_date_from",$registration_date_from);
				$this->view->assign("registration_date_to",$registration_date_to);
				$this->view->assign('sltProfession',$sltProfession);
				$this->view->assign('sltApplication',$sltApplication);
				
		        $this->view->assign('pageIndex', $pageIndex);
		        $this->view->assign('enterprise', $enterprise);
		        $this->view->assign('numEnterprise', $total);
		
		        $this->view->assign('paginator', $paginator);
		        $this->view->assign('paginatorOptions', array(
		            'path'        => $this->view->url(array(), 'enterprise_enterprise_list'),
		            'itemLink' => (null == $params)
		                            ? 'page-%d'
		                            : 'page-%d/' . '?q=' . $params,
		        ));
		        
		        /**********************************
				*	2. Danh sach san pham
				**********************************/
		        
		        /**
		         * Product model acess to data
		         **/
		        $productDao = Tomato_Model_Dao_Factory::getInstance()->setModule('shop')->getProductDao();
		        $productDao->setDbConnection($conn);
		
		        /**
		         * Product content access to data
		         **/
		        $productContentDao = Tomato_Model_Dao_Factory::getInstance()->setModule('shop')->getProductContentDao();
		        $productContentDao->setDbConnection($conn);
		        
				/**
		         * Category model access to data
		        **/
		        $categoryDao = Tomato_Model_Dao_Factory::getInstance()->setModule('shop')->getCategoryDao();
		        $categoryDao->setDbConnection($conn);
		        
		        /**
		        * Get category information to tree view
		        **/
		        $categories = $categoryDao->getCategoryTree();
		        $this->view->assign('categories', $categories);
		        
		        $request    = $this->getRequest();
		        $pageIndex  = $this->_request->getParam('pageIndex', 1);
		        $perPage    = 20;
		        $offset     = ($pageIndex - 1) * $perPage;
		        $purifier     = new HTMLPurifier();
		
		        /**
		         * Build product search expression
		         **/
		        $params = null;
		        $exp    = array();
		
		        /**
		        * Check permission all list enterprise
		        */
		        if(!Core_Services_RuleChecker::isAllowed('all-list')){
		            $exp['enterprise_id'] = $purifier->purify(@$userAccount->enterprise_id);
		        }
		
		        if ($request->isPost()) {
		            $categoryId   = $request->getPost('category');
		            if ($categoryId) {
		                $exp['category_id'] = $categoryId;
		            }
		            $params = rawurlencode(base64_encode(Zend_Json::encode($exp)));
		        } else {
		            $params = $request->getParam('q');
		            if (null != $params) {
		                $exp = rawurldecode(base64_decode($params));
		                $exp = Zend_Json::decode($exp);
		            } else {
		                $params = rawurlencode(base64_encode(Zend_Json::encode($exp)));
		            }
		        }
		
		        /**
		        * Get list product and count all product to paginator
		        * $offset: position start
		        * $perPage: item pager
		        * $exp: condition
		        **/
		        $products   = $productDao->find($offset, $perPage, $exp);
		        $numProduct = $productDao->count($exp);
		
		        /**
		         * Get language content each product
		         * $arrPoductContentName: to assign language information foreach match product
		         **/
		        $arrPoductContentName = array();
		        foreach ($products as $row){
		            $product_content = $productContentDao->getProductContentByProductId($row->product_id, "pc.language_code DESC");
		            $strPoductContentName = '';
		            $i = 0;
		            foreach ($product_content as $row1){
		                $strBr = null;
		                if($i >= 1){
		                    $strBr = "<br/>";
		                }
		                $strPoductContentName .= $strBr.$row1->product_content_name." ({$row1->language_name})";
		                $i++;
		            }
		            $arrPoductContentName[$row->product_id] = $strPoductContentName;
		        }
		        $this->view->assign('arrPoductContentName', $arrPoductContentName);
		
		        $this->view->assign('product', $products);
		        $this->view->assign('numProduct', $numProduct);
		        $this->view->assign('exp', $exp);
		        $this->view->assign('pageIndex', $pageIndex);
		        
		        /**********************************
				*	3. Danh sach don dat hang
				**********************************/
		        
		        /**
		         * Order model access to data
		         **/
		        $orderDao = Tomato_Model_Dao_Factory::getInstance()->setModule('shop')->getOrderDao();
		        $orderDao->setDbConnection($conn);
		        
		        /**
		         * Assignment access to db
		         */
		        $assignmentDao = Tomato_Model_Dao_Factory::getInstance()->setModule('assignment')->getAssignmentDao();
		        $assignmentDao->setDbConnection($conn);
		        
		        /**
		         * Evaluation basic model access to data
		         */
		        $evaluationDao = Tomato_Model_Dao_Factory::getInstance()->setModule('shop')->getEvaluationDao();
		        $evaluationDao->setDbConnection($conn);
		        
		        /**
		        * Core User model access to db
		        */
		        $userDao = Tomato_Model_Dao_Factory::getInstance()->setModule('core')->getUserDao();
		        $userDao->setDbConnection($conn);
		        
		        $pageIndex = $request->getParam('pageIndex', 1);
		        $perPage   = 20;
		        $offset    = ($pageIndex - 1) * $perPage;
		        $purifier     = new HTMLPurifier();
		
		        /**
		         * Build product search expression
		         **/
		        $params = null;
		        $exp = array();
		
		        /**
		        * Check permission all list assignment
		        */
		        if(!Core_Services_RuleChecker::isAllowed('all-list')){
		            if($userAccount){
		                $enterprise_id = (null != $userAccount->enterprise_id)?$userAccount->enterprise_id:null;
		                $exp['enterprise_id'] = $purifier->purify($enterprise_id);
		            }else{
		                die('Not found enterpise id');
		            }
		        }
		
		        /**
		         * Get all process of tracking
		         * $arrStatusIdOfAssignment: Store all status of process tracking
		         **/
		        $a_process = $assignmentDao->getPByType(2);
		        $arrStatusIdOfAssignment = array();
		        foreach($a_process as $a_pro){
		            $arrStatusIdOfAssignment[$a_pro->status_id] = $a_pro->status_id;
		        }
		
		        /**
		         * Get status for filter selectbox
		         * $arrSStatus: store status info
		         **/
		        $arrSStatus = array();
		        if($arrStatusIdOfAssignment){
		            foreach($arrStatusIdOfAssignment as $s_id){
		                $ojStatus = $assignmentDao->getSSById($s_id);
		                $arrSStatus[] = $ojStatus;
		            }
		        }
		        $this->view->assign('arrSStatus', $arrSStatus);
		
		        /**
		        * Filter
		        **/
		        if($request->isPost()){
		            $order_created_form_date = $request->getParam('order_created_form_date');
		            $order_created_to_date   = $request->getParam('order_created_to_date');
		            $order_tracking_status   = $request->getParam('order_tracking_status');
		            $order_status            = $request->getParam('order_status');
		
		            /**
		            * Check condition filter
		            **/
		            if('all' != $order_tracking_status){
		                $exp['order_tracking_status'] = $order_tracking_status;
		                $f_process = $assignmentDao->getPBySId($order_tracking_status);
		                if(count($f_process) > 0){
		                    $str_cond = null;
		                    $i = 0;
		                    foreach($f_process as $p_row){
		                        $cond = "o.`process_id` = {$p_row->process_id}";
		                        $cond1 = " or o.`process_id` = {$p_row->process_id}";
		                        $str_cond .= ($i==0)?$cond:$cond1;
		                        $i++;
		                    }
		                    $exp['process_id'] = $str_cond;
		                }
		            }
		
		            if($order_created_form_date){
		                $order_created_form_date = date('Y-m-d',strtotime($this->convertDateFormat($order_created_form_date)));
		                $exp['order_created_form_date'] = $order_created_form_date;
		            }
		            if($order_created_to_date){
		                $order_created_to_date = date('Y-m-d',strtotime($this->convertDateFormat($order_created_to_date)));
		                $exp['order_created_to_date'] = $order_created_to_date;
		            }
		            if('all' != $order_status){
		                $exp['order_status'] = $order_status;
		            }
		
		            $params = rawurlencode(base64_encode(Zend_Json::encode($exp)));
		        }
		        else {
		            $params = $request->getParam('q');
		            if (null != $params) {
		                $exp = rawurldecode(base64_decode($params));
		                $exp = Zend_Json::decode($exp);
		            } else {
		                $params = rawurlencode(base64_encode(Zend_Json::encode($exp)));
		            }
		        }
		
		        $order = $orderDao->find($offset, $perPage, $exp);
		        $numOrder = $orderDao->count($exp);
		
		        /**
		         * $arrCustomer: array object customer info
		         * $arrStatus: array object status
		         * $arrEName : array enterprise name of each user
		        **/
		        $arrCustomer = array();
		        $arrStatus = array();
		        $arrEName = array();
		        $arrEvalOrders = array();
		        foreach($order as $r_order){
		
		            /**
		            * Get customer info for order
		            **/
		            $objuser = $userDao->getById($r_order->customer_id);
		            $arrCustomer[$r_order->order_id] = $objuser;
		            if($objuser){
		                $objEU = $userAccountDao->getByUser($objuser->user_id);
		                if($objEU){
		                    $enterprise = $enterpriseDao->getById($objEU->enterprise_id);
		                    $arrEName[$r_order->order_id.'_'.$objuser->user_id] = $enterprise->company_name_en;
		                }
		            }
		
		            /**
		            * Get tracking status info
		            **/
		            if($r_order->process_id){
		                $status = $assignmentDao->getPSById($r_order->process_id);
		                $arrStatus[$r_order->order_id] = $status->code;
		            }
		
		            /**
		             * Check seller or buyer no evaluated yet
		            **/
		            $eval_exp['order_id'] = $r_order->order_id;
		            $eval_orders = $evaluationDao->getById($eval_exp);
		            $arrEvalOrders[$r_order->order_id] = $eval_orders;
		        }
		        $this->view->assign('arrCustomer', $arrCustomer);
		        $this->view->assign('arrStatus', $arrStatus);
		        $this->view->assign('arrEName', $arrEName);
		        $this->view->assign('arrEvalOrders', $arrEvalOrders);
		
		        /** Paginator*/
		        $paginator = new Zend_Paginator(new Tomato_Utility_PaginatorAdapter($order, $numOrder));
		        $paginator->setCurrentPageNumber($pageIndex);
		        $paginator->setItemCountPerPage($perPage);
		
		        $this->view->assign('exp', $exp);
		        $this->view->assign('pageIndex', $pageIndex);
		        $this->view->assign('order', $order);
		        $this->view->assign('numOrder', $numOrder);
		
		        $this->view->assign('paginator', $paginator);
		        $this->view->assign('paginatorOptions', array(
		            'path'        => $this->view->url(array(), 'order_order_list'),
		            'itemLink' => (null == $params)
		                            ? 'page-%d/'
		                            : 'page-%d/?q=' . $params,
		        ));
		        
		        /**********************************
				*	4. Danh sach cong viec
				**********************************/
		        
		        $pageIndex = $request->getParam('pageIndex', 1);
		        $perPage   = 20;
		        $offset    = ($pageIndex - 1) * $perPage;
		        $purifier     = new HTMLPurifier();
		
		        /**
		         * Build product search expression
		         **/
		        $params = null;
		        $exp = array();
		
		        /**
		        * Get current user
		        */
		        $user = Zend_Auth::getInstance()->getIdentity();
		        $userAccount = $userAccountDao->getByUser($user->user_id);
		
		        /**
		         * Get information for filter
		         * $a_process: array object assignment process with type = 1
		         * $role: Role of producer
		        **/
		        $a_process = $assignmentDao->getPByType(1);
		        $arrStatusIdOfAssignment = array();
		        foreach($a_process as $a_pro){
		            $arrStatusIdOfAssignment[$a_pro->status_id] = $a_pro->status_id;
		        }
		
		        /**
		         * Get status for filter selectbox
		         * $arrSStatus: store status info
		         **/
		        $arrSStatus = array();
		        if($arrStatusIdOfAssignment){
		            foreach($arrStatusIdOfAssignment as $s_id){
		                $ojStatus = $assignmentDao->getSSById($s_id);
		                $arrSStatus[] = $ojStatus;
		            }
		        }
		        $this->view->assign('arrSStatus', $arrSStatus);
		
		        /**
		         * Get all producer to selectbox for assignment
		        **/
		        $role = $assignmentDao->getRoleByName('nhan vien san xuat');
		        $arrSProducer = array();
		        if(isset($userAccount->enterprise_id)){
		            $userOfEnterPrise = $userAccountDao->getUserAccountBy($userAccount->enterprise_id);
		            foreach($userOfEnterPrise as $e_user){
		                if($role->role_id == $e_user->role_id){
		                    $arrSProducer[] = $e_user;
		                }
		            }
		        }
		        $this->view->assign('arrSProducer', $arrSProducer);
		
		        /**
		        * Check permission all list assignment
		        */
		        if(!Core_Services_RuleChecker::isAllowed('all-list')){
		            if($userAccount){
		                $enterprise_id = (null != $userAccount->enterprise_id)?$userAccount->enterprise_id:null;
		                $exp['enterprise_id'] = $purifier->purify($enterprise_id);
		            }else{
		                die('Not found enterpise id');
		            }
		        }
		
		        /**
		        * Check permission all list assignment of company
		        */
		        if(!Core_Services_RuleChecker::isAllowed('company-list')){
		            if($user){
		                $u_id = $user->user_id;
		                $exp['producer_account_id'] = $u_id;
		                $exp['assigner_account_id'] = $u_id;
		            }
		        }
		
		        if($request->isPost()){
		            $assignment_status   = $request->getPost('assignment_status');
		            $from_create_date    = $request->getPost('from_create_date');
		            $to_create_date      = $request->getPost('to_create_date');
		            $from_due_date       = $request->getPost('from_due_date');
		            $to_due_date         = $request->getPost('to_due_date');
		            $assignment_producer = $request->getPost('assignment_producer');
		            $order_invoice       = $request->getPost('order_invoice');
		            $sort_product_name   = $request->getPost('sort_product_name');
		            $sort_create_date    = $request->getPost('sort_create_date');
		
		            /**
		            * Check condition filter
		            **/
		            if('all' != $assignment_status){
		                $exp['assignment_status'] = $assignment_status;
		                $f_process = $assignmentDao->getPBySId($assignment_status);
		                if(count($f_process) > 0){
		                    $str_cond = null;
		                    $i = 0;
		                    foreach($f_process as $p_row){
		                        $cond = "a.`process_id` = {$p_row->process_id}";
		                        $cond1 = " or a.`process_id` = {$p_row->process_id}";
		                        $str_cond .= ($i==0)?$cond:$cond1;
		                        $i++;
		                    }
		                    $exp['process_id'] = $str_cond;
		                }
		            }
		            if($from_create_date){
		                $from_create_date = date('Y-m-d',strtotime($this->convertDateFormat($from_create_date)));
		                $exp['assignment_create_from_date'] = $from_create_date;
		            }
		            if($to_create_date){
		                $to_create_date = date('Y-m-d',strtotime($this->convertDateFormat($to_create_date)));
		                $exp['assignment_create_to_date'] = $to_create_date;
		            }
		            if($from_due_date){
		                $from_due_date = date('Y-m-d',strtotime($this->convertDateFormat($from_due_date)));
		                $exp['assignment_due_from_date'] = $from_due_date;
		            }
		            if($to_due_date){
		                $to_due_date = date('Y-m-d',strtotime($this->convertDateFormat($to_due_date)));
		                $exp['assignment_due_to_date'] = $to_due_date;
		            }
		            if('all' != $assignment_producer){
		                $exp['assignment_producer'] = $assignment_producer;
		            }
		            if($order_invoice){
		                $exp['order_invoice'] = $order_invoice;
		            }
		            if($sort_product_name){
		                $exp['sort_product_name'] = $sort_product_name;
		            }
		            if($sort_create_date){
		                $exp['sort_create_date'] = $sort_create_date;
		            }
		            $params = rawurlencode(base64_encode(Zend_Json::encode($exp)));
		        }
		        else {
		            $params = $request->getParam('q');
		            if (null != $params) {
		                $exp = rawurldecode(base64_decode($params));
		                $exp = Zend_Json::decode($exp);
		            } else {
		                $params = rawurlencode(base64_encode(Zend_Json::encode($exp)));
		            }
		        }
		        $assignments = $assignmentDao->find($offset, $perPage, $exp);
		        $total       = $assignmentDao->count($exp);
		
		        /**
		         * Declaration variable and assign value default
		         * $arrProduct: Store product info for assignment
		         * $arrProducer: Store producer info for assignment
		         * $arrAssigner: Store assigner info for assignment
		         * $arrDueDate: Store due date for assignment
		         * $arrDueDate: Store status name of assignment
		         * $arrAssignments : Store all assignment after check due_date sorting
		        **/
		        $arrProduct     = array();
		        $arrProducer    = array();
		        $arrAssigner    = array();
		        $arrDueDate     = array();
		        $arrStatus      = array();
		        $arrAssignments = array();
		
		        $index = 0;
		        foreach($assignments as $assignment){
		            /**
		            * Get product info for assignment
		            **/
		            if($assignment->order_detail_id){
		                $product = $assignmentDao->getODById($assignment->order_detail_id);
		                $arrProduct[$assignment->assignment_id] = $product;
		            }
		
		            /**
		             * Get producer info for asssignment
		            **/
		            if($assignment->producer_account_id){
		                $producer = $userDao->getById($assignment->producer_account_id);
		                $arrProducer[$assignment->assignment_id] = $producer;
		            }
		
		            /**
		             * Get assigner info for asssignment
		            **/
		            if($assignment->assigner_account_id){
		                $assigner = $userDao->getById($assignment->assigner_account_id);
		                $arrAssigner[$assignment->assignment_id] = $assigner;
		            }
		
		            /**
		             * Calculated due date
		            **/
		            /*$dueDate = null;
		            if(isset($product)){
		                if($product->production_duration){
		                    $numDay   = $product->production_duration;
		                    $seconds  = $numDay*24*60*60;
		                    $intTime  = strtotime($assignment->assignment_create_date)+$seconds;
		                    $dueDate = date("d-m-Y",$intTime);
		                }
		            }
		            $arrDueDate[$assignment->assignment_id] = $dueDate;*/
		
		            /**
		             * Get status for assignment
		            **/
		            if($assignment->process_id){
		                $status = $assignmentDao->getPSById($assignment->process_id);
		                $arrStatus[$assignment->assignment_id] = $status->code;
		            }
		
		            /**
		             * Create array assignment to sort
		             **/
		            $arrAssignments[$index][0] = $product->product_admin_name;
		            $arrAssignments[$index][1] = $assignment->assignment_create_date;
		            $arrAssignments[$index][2] = $assignment;
		            $index++;
		        }
		        $this->view->assign('arrProduct', $arrProduct);
		        $this->view->assign('arrProducer', $arrProducer);
		        $this->view->assign('arrAssigner', $arrAssigner);
		        $this->view->assign('arrDueDate', $arrDueDate);
		        $this->view->assign('arrStatus', $arrStatus);
		
		        /**
		        * Storting with product name or create date
		        **/
		        if(isset($exp['sort_create_date'])){
		            $arrAssignments = $this->array_sort($arrAssignments, 1);
		        }
		        if(isset($exp['sort_product_name'])){
		            $arrAssignments = $this->array_sort($arrAssignments, 0, 'SORT_ASC');
		        }
		        $arrAssignmentsSorted = array();
		        foreach($arrAssignments as $r_assignment){
		            $arrAssignmentsSorted[] = $r_assignment[2];
		        }
		
		        /**
		        * Paginator
		        */
		        $paginator = new Zend_Paginator(new Tomato_Utility_PaginatorAdapter($assignments, $total));
		        $paginator->setCurrentPageNumber($pageIndex);
		        $paginator->setItemCountPerPage($perPage);
		
		        $this->view->assign('exp', $exp);
		        $this->view->assign('pageIndex', $pageIndex);
		        $this->view->assign('assignments', $arrAssignmentsSorted);
		        $this->view->assign('numAssignment', $total);
		
		        $this->view->assign('paginator', $paginator);
		        $this->view->assign('paginatorOptions', array(
		            'path'        => $this->view->url(array(), 'assignment_assignment_list'),
		            'itemLink' => (null == $params)
		                            ? 'page-%d/'
		                            : 'page-%d/?q=' . $params,
		        ));
		        
				/** End: Thong tin DN - SP moi nhat - Danh sach don dat hang - Assignment **/
				break;
		}
	}
}
